package com.wuchao.reflect;

import java.lang.reflect.*;
import java.util.Arrays;

/**
 * @Author wuchao66
 * @Date 2023/10/24 14:10
 * @PackageName:com.wuchao.reflect
 * @ClassName: T
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
public class T {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
        //如何反射到实例
        Class  s1= Class.forName("com.wuchao.reflect.Student");
        Class<? extends Student> c2 = new Student().getClass();
        Class<Student> s3 = Student.class;//******
        Student s  = new Student();
        s.show();
        //s3本质上他还是一个反射得到的伪类
        Student sss = s3.newInstance();//这才是反射得到的实例化
        //构造函数 属性
        //属性 所有
        Field[] declaredFields = s3.getDeclaredFields();
        Arrays.asList(declaredFields).forEach(xjj-> System.out.println(xjj.getName()));
        Field[] declaredFields1 = s3.getFields();//公有
        Arrays.asList(declaredFields1).forEach(xjj-> System.out.println(xjj.getName()));
        //拿到私有名字
//        Field name = s3.getDeclaredField("name");
//        System.out.println();
        sss.setName("我是私有name");
        System.out.println(sss.show());
        //构造函数
        Constructor[] declaredConstructors = s3.getDeclaredConstructors();
        Arrays.asList(declaredConstructors).forEach(xjj-> System.out.println(xjj.getName()));
        //进一步分析里面会有那些参数和位置
        for(Constructor xjj : declaredConstructors){
            System.out.println(xjj.getParameterCount()+"\t");
            for(Object xjjj : xjj.getParameters()){
                System.out.println(xjjj);
            }
            for(Object xjb : xjj.getParameterTypes()){
                System.out.println(xjb);
            }
        }
        Constructor<Student> declaredConstructor = s3.getDeclaredConstructor(Integer.class, String.class);
       // new Student(1,"xxxx") 得到正的
        // 方法
        Method[] declaredMethods = s3.getDeclaredMethods();
        Arrays.asList(declaredMethods).forEach(xjj-> System.out.println(xjj.getName()));
        for(Method xjj : declaredMethods){
            System.out.println("方法名"+xjj.getName());
            System.out.println("入参个数："+xjj.getParameterCount()+"\t");
            System.out.println("返回值类型："+xjj.getReturnType().getName());
            for(Object xjb : xjj.getParameterTypes()){
                System.out.println(xjb);
            }
        }
        Method show = s3.getDeclaredMethod("show", String.class);
        //召唤回调
        show.invoke(sss,"我反调");
    }

}
